home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / shell13s.zoo / shell1.3 / lib / posixtm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  13.0 KB  |  642 lines

  1.  
  2. # line 20 "posixtm.y"
  3. #ifdef __GNUC__
  4. #define alloca __builtin_alloca
  5. #else
  6. #ifdef sparc
  7. #include <alloca.h>
  8. #else
  9. #ifdef _AIX
  10.  #pragma alloca
  11. #else
  12. char *alloca ();
  13. #endif
  14. #endif
  15. #endif
  16. # define digit 257
  17.  
  18. # line 38 "posixtm.y"
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <time.h>
  22.  
  23. #define YYDEBUG 1
  24.  
  25. static char *curpos;
  26.  
  27. static struct tm t;
  28.  
  29. time_t mktime ();
  30.  
  31. static int zzlex ();
  32. #define zzparse posixtime_zzparse
  33. static int zzerror ();
  34. #define zzclearin zzchar = -1
  35. #define zzerrok zzerrflag = 0
  36. extern int zzchar;
  37. extern int zzerrflag;
  38. #ifndef YYMAXDEPTH
  39. #define YYMAXDEPTH 150
  40. #endif
  41. #ifndef YYSTYPE
  42. #define YYSTYPE int
  43. #endif
  44. YYSTYPE zzlval, zzval;
  45. typedef int zztabelem;
  46. # define YYERRCODE 256
  47.  
  48. # line 126 "posixtm.y"
  49.  
  50. static int
  51. zzlex ()
  52. {
  53.   char ch = *curpos++;
  54.  
  55.   if (ch >= '0' && ch <= '9')
  56.     {
  57.       zzlval = ch - '0';
  58.       return digit;
  59.     }
  60.   else if (ch == '.' || ch == '\0')
  61.     return ch;
  62.   else
  63.     /* Cause an error.  */
  64.     return '?';
  65. }
  66.  
  67. static int
  68. zzerror ()
  69. {
  70.   return 0;
  71. }
  72.  
  73. /* Parse a POSIX-style date and return it, or (time_t)-1 for an error.  */
  74.  
  75. time_t
  76. posixtime (s)
  77.      char *s;
  78. {
  79.   curpos = s;
  80.   /* Let mktime() decide whether it is daylight savings time.  */
  81.   t.tm_isdst = -1;
  82.   if (zzparse ())
  83.     return (time_t)-1;
  84.   else
  85.     return mktime (&t);
  86. }
  87.  
  88. /* Parse a POSIX-style date and return it, or NULL for an error.  */
  89.  
  90. struct tm *
  91. posixtm (s)
  92.      char *s;
  93. {
  94.   if (posixtime (s) == -1)
  95.     return NULL;
  96.   return &t;
  97. }
  98. zztabelem zzexca[] ={
  99. -1, 1,
  100.     0, -1,
  101.     -2, 0,
  102.     };
  103. # define YYNPROD 8
  104. # define YYLAST 16
  105. zztabelem zzact[]={
  106.  
  107.      3,     5,    11,     2,    10,     8,     4,     1,     6,     0,
  108.      7,     9,     0,    12,     0,    13 };
  109. zztabelem zzpact[]={
  110.  
  111.   -257, -1000,  -257,  -256,  -257, -1000,  -257,  -257,   -44,  -257,
  112.  -1000,  -257, -1000, -1000 };
  113. zztabelem zzpgo[]={
  114.  
  115.      0,     7,     3,     5,     4 };
  116. zztabelem zzr1[]={
  117.  
  118.      0,     1,     3,     3,     3,     4,     4,     2 };
  119. zztabelem zzr2[]={
  120.  
  121.      0,    13,     3,     5,     1,     1,     5,     5 };
  122. zztabelem zzchk[]={
  123.  
  124.  -1000,    -1,    -2,   257,    -2,   257,    -2,    -2,    -3,    -2,
  125.     -4,    46,    -2,    -2 };
  126. zztabelem zzdef[]={
  127.  
  128.      0,    -2,     0,     0,     0,     7,     0,     4,     5,     2,
  129.      1,     0,     3,     6 };
  130. typedef struct { char *t_name; int t_val; } zztoktype;
  131. #ifndef YYDEBUG
  132. #    define YYDEBUG    0    /* don't allow debugging */
  133. #endif
  134.  
  135. #if YYDEBUG
  136.  
  137. zztoktype zztoks[] =
  138. {
  139.     "digit",    257,
  140.     "-unknown-",    -1    /* ends search */
  141. };
  142.  
  143. char * zzreds[] =
  144. {
  145.     "-no such reduction-",
  146.     "date : digitpair digitpair digitpair digitpair year seconds",
  147.     "year : digitpair",
  148.     "year : digitpair digitpair",
  149.     "year : /* empty */",
  150.     "seconds : /* empty */",
  151.     "seconds : '.' digitpair",
  152.     "digitpair : digit digit",
  153. };
  154. #endif /* YYDEBUG */
  155. /*    @(#)yaccpar    1.10    */
  156. /*
  157. ** Skeleton parser driver for yacc output
  158. */
  159.  
  160. /*
  161. ** yacc user known macros and defines
  162. */
  163. #define YYERROR        goto zzerrlab
  164. #define YYACCEPT    return(0)
  165. #define YYABORT        return(1)
  166. #define YYBACKUP( newtoken, newvalue )\
  167. {\
  168.     if ( zzchar >= 0 || ( zzr2[ zztmp ] >> 1 ) != 1 )\
  169.     {\
  170.         zzerror( "syntax error - cannot backup" );\
  171.         goto zzerrlab;\
  172.     }\
  173.     zzchar = newtoken;\
  174.     zzstate = *zzps;\
  175.     zzlval = newvalue;\
  176.     goto zznewstate;\
  177. }
  178. #define YYRECOVERING()    (!!zzerrflag)
  179. #ifndef YYDEBUG
  180. #    define YYDEBUG    1    /* make debugging available */
  181. #endif
  182.  
  183. /*
  184. ** user known globals
  185. */
  186. int zzdebug;            /* set to 1 to get debugging */
  187.  
  188. /*
  189. ** driver internal defines
  190. */
  191. #define YYFLAG        (-1000)
  192.  
  193. /*
  194. ** global variables used by the parser
  195. */
  196. YYSTYPE zzv[ YYMAXDEPTH ];    /* value stack */
  197. int zzs[ YYMAXDEPTH ];        /* state stack */
  198.  
  199. YYSTYPE *zzpv;            /* top of value stack */
  200. int *zzps;            /* top of state stack */
  201.  
  202. int zzstate;            /* current state */
  203. int zztmp;            /* extra var (lasts between blocks) */
  204.  
  205. int zznerrs;            /* number of errors */
  206. int zzerrflag;            /* error recovery flag */
  207. int zzchar;            /* current input token number */
  208.  
  209.  
  210.  
  211. /*
  212. ** zzparse - return 0 if worked, 1 if syntax error not recovered from
  213. */
  214. int
  215. zzparse()
  216. {
  217.     register YYSTYPE *zzpvt;    /* top of value stack for $vars */
  218.  
  219.     /*
  220.     ** Initialize externals - zzparse may be called more than once
  221.     */
  222.     zzpv = &zzv[-1];
  223.     zzps = &zzs[-1];
  224.     zzstate = 0;
  225.     zztmp = 0;
  226.     zznerrs = 0;
  227.     zzerrflag = 0;
  228.     zzchar = -1;
  229.  
  230.     goto zzstack;
  231.     {
  232.         register YYSTYPE *zz_pv;    /* top of value stack */
  233.         register int *zz_ps;        /* top of state stack */
  234.         register int zz_state;        /* current state */
  235.         register int  zz_n;        /* internal state number info */
  236.  
  237.         /*
  238.         ** get globals into registers.
  239.         ** branch to here only if YYBACKUP was called.
  240.         */
  241.     zznewstate:
  242.         zz_pv = zzpv;
  243.         zz_ps = zzps;
  244.         zz_state = zzstate;
  245.         goto zz_newstate;
  246.  
  247.         /*
  248.         ** get globals into registers.
  249.         ** either we just started, or we just finished a reduction
  250.         */
  251.     zzstack:
  252.         zz_pv = zzpv;
  253.         zz_ps = zzps;
  254.         zz_state = zzstate;
  255.  
  256.         /*
  257.         ** top of for (;;) loop while no reductions done
  258.         */
  259.     zz_stack:
  260.         /*
  261.         ** put a state and value onto the stacks
  262.         */
  263. #if YYDEBUG
  264.         /*
  265.         ** if debugging, look up token value in list of value vs.
  266.         ** name pairs.  0 and negative (-1) are special values.
  267.         ** Note: linear search is used since time is not a real
  268.         ** consideration while debugging.
  269.         */
  270.         if ( zzdebug )
  271.         {
  272.             register int zz_i;
  273.  
  274.             printf( "State %d, token ", zz_state );
  275.             if ( zzchar == 0 )
  276.                 printf( "end-of-file\n" );
  277.             else if ( zzchar < 0 )
  278.                 printf( "-none-\n" );
  279.             else
  280.             {
  281.                 for ( zz_i = 0; zztoks[zz_i].t_val >= 0;
  282.                     zz_i++ )
  283.                 {
  284.                     if ( zztoks[zz_i].t_val == zzchar )
  285.                         break;
  286.                 }
  287.                 printf( "%s\n", zztoks[zz_i].t_name );
  288.             }
  289.         }
  290. #endif /* YYDEBUG */
  291.         if ( ++zz_ps >= &zzs[ YYMAXDEPTH ] )    /* room on stack? */
  292.         {
  293.             zzerror( "yacc stack overflow" );
  294.             YYABORT;
  295.         }
  296.         *zz_ps = zz_state;
  297.         *++zz_pv = zzval;
  298.  
  299.         /*
  300.         ** we have a new state - find out what to do
  301.         */
  302.     zz_newstate:
  303.         if ( ( zz_n = zzpact[ zz_state ] ) <= YYFLAG )
  304.             goto zzdefault;        /* simple state */
  305. #if YYDEBUG
  306.         /*
  307.         ** if debugging, need to mark whether new token grabbed
  308.         */
  309.         zztmp = zzchar < 0;
  310. #endif
  311.         if ( ( zzchar < 0 ) && ( ( zzchar = zzlex() ) < 0 ) )
  312.             zzchar = 0;        /* reached EOF */
  313. #if YYDEBUG
  314.         if ( zzdebug && zztmp )
  315.         {
  316.             register int zz_i;
  317.  
  318.             printf( "Received token " );
  319.             if ( zzchar == 0 )
  320.                 printf( "end-of-file\n" );
  321.             else if ( zzchar < 0 )
  322.                 printf( "-none-\n" );
  323.             else
  324.             {
  325.                 for ( zz_i = 0; zztoks[zz_i].t_val >= 0;
  326.                     zz_i++ )
  327.                 {
  328.                     if ( zztoks[zz_i].t_val == zzchar )
  329.                         break;
  330.                 }
  331.                 printf( "%s\n", zztoks[zz_i].t_name );
  332.             }
  333.         }
  334. #endif /* YYDEBUG */
  335.         if ( ( ( zz_n += zzchar ) < 0 ) || ( zz_n >= YYLAST ) )
  336.             goto zzdefault;
  337.         if ( zzchk[ zz_n = zzact[ zz_n ] ] == zzchar )    /*valid shift*/
  338.         {
  339.             zzchar = -1;
  340.             zzval = zzlval;
  341.             zz_state = zz_n;
  342.             if ( zzerrflag > 0 )
  343.                 zzerrflag--;
  344.             goto zz_stack;
  345.         }
  346.  
  347.     zzdefault:
  348.         if ( ( zz_n = zzdef[ zz_state ] ) == -2 )
  349.         {
  350. #if YYDEBUG
  351.             zztmp = zzchar < 0;
  352. #endif
  353.             if ( ( zzchar < 0 ) && ( ( zzchar = zzlex() ) < 0 ) )
  354.                 zzchar = 0;        /* reached EOF */
  355. #if YYDEBUG
  356.             if ( zzdebug && zztmp )
  357.             {
  358.                 register int zz_i;
  359.  
  360.                 printf( "Received token " );
  361.                 if ( zzchar == 0 )
  362.                     printf( "end-of-file\n" );
  363.                 else if ( zzchar < 0 )
  364.                     printf( "-none-\n" );
  365.                 else
  366.                 {
  367.                     for ( zz_i = 0;
  368.                         zztoks[zz_i].t_val >= 0;
  369.                         zz_i++ )
  370.                     {
  371.                         if ( zztoks[zz_i].t_val
  372.                             == zzchar )
  373.                         {
  374.                             break;
  375.                         }
  376.                     }
  377.                     printf( "%s\n", zztoks[zz_i].t_name );
  378.                 }
  379.             }
  380. #endif /* YYDEBUG */
  381.             /*
  382.             ** look through exception table
  383.             */
  384.             {
  385.                 register int *zzxi = zzexca;
  386.  
  387.                 while ( ( *zzxi != -1 ) ||
  388.                     ( zzxi[1] != zz_state ) )
  389.                 {
  390.                     zzxi += 2;
  391.                 }
  392.                 while ( ( *(zzxi += 2) >= 0 ) &&
  393.                     ( *zzxi != zzchar ) )
  394.                     ;
  395.                 if ( ( zz_n = zzxi[1] ) < 0 )
  396.                     YYACCEPT;
  397.             }
  398.         }
  399.  
  400.         /*
  401.         ** check for syntax error
  402.         */
  403.         if ( zz_n == 0 )    /* have an error */
  404.         {
  405.             /* no worry about speed here! */
  406.             switch ( zzerrflag )
  407.             {
  408.             case 0:        /* new error */
  409.                 zzerror( "syntax error" );
  410.                 goto skip_init;
  411.             zzerrlab:
  412.                 /*
  413.                 ** get globals into registers.
  414.                 ** we have a user generated syntax type error
  415.                 */
  416.                 zz_pv = zzpv;
  417.                 zz_ps = zzps;
  418.                 zz_state = zzstate;
  419.                 zznerrs++;
  420.             skip_init:
  421.             case 1:
  422.             case 2:        /* incompletely recovered error */
  423.                     /* try again... */
  424.                 zzerrflag = 3;
  425.                 /*
  426.                 ** find state where "error" is a legal
  427.                 ** shift action
  428.                 */
  429.                 while ( zz_ps >= zzs )
  430.                 {
  431.                     zz_n = zzpact[ *zz_ps ] + YYERRCODE;
  432.                     if ( zz_n >= 0 && zz_n < YYLAST &&
  433.                         zzchk[zzact[zz_n]] == YYERRCODE)                    {
  434.                         /*
  435.                         ** simulate shift of "error"
  436.                         */
  437.                         zz_state = zzact[ zz_n ];
  438.                         goto zz_stack;
  439.                     }
  440.                     /*
  441.                     ** current state has no shift on
  442.                     ** "error", pop stack
  443.                     */
  444. #if YYDEBUG
  445. #    define _POP_ "Error recovery pops state %d, uncovers state %d\n"
  446.                     if ( zzdebug )
  447.                         printf( _POP_, *zz_ps,
  448.                             zz_ps[-1] );
  449. #    undef _POP_
  450. #endif
  451.                     zz_ps--;
  452.                     zz_pv--;
  453.                 }
  454.                 /*
  455.                 ** there is no state on stack with "error" as
  456.                 ** a valid shift.  give up.
  457.                 */
  458.                 YYABORT;
  459.             case 3:        /* no shift yet; eat a token */
  460. #if YYDEBUG
  461.                 /*
  462.                 ** if debugging, look up token in list of
  463.                 ** pairs.  0 and negative shouldn't occur,
  464.                 ** but since timing doesn't matter when
  465.                 ** debugging, it doesn't hurt to leave the
  466.                 ** tests here.
  467.                 */
  468.                 if ( zzdebug )
  469.                 {
  470.                     register int zz_i;
  471.  
  472.                     printf( "Error recovery discards " );
  473.                     if ( zzchar == 0 )
  474.                         printf( "token end-of-file\n" );
  475.                     else if ( zzchar < 0 )
  476.                         printf( "token -none-\n" );
  477.                     else
  478.                     {
  479.                         for ( zz_i = 0;
  480.                             zztoks[zz_i].t_val >= 0;
  481.                             zz_i++ )
  482.                         {
  483.                             if ( zztoks[zz_i].t_val
  484.                                 == zzchar )
  485.                             {
  486.                                 break;
  487.                             }
  488.                         }
  489.                         printf( "token %s\n",
  490.                             zztoks[zz_i].t_name );
  491.                     }
  492.                 }
  493. #endif /* YYDEBUG */
  494.                 if ( zzchar == 0 )    /* reached EOF. quit */
  495.                     YYABORT;
  496.                 zzchar = -1;
  497.                 goto zz_newstate;
  498.             }
  499.         }/* end if ( zz_n == 0 ) */
  500.         /*
  501.         ** reduction by production zz_n
  502.         ** put stack tops, etc. so things right after switch
  503.         */
  504. #if YYDEBUG
  505.         /*
  506.         ** if debugging, print the string that is the user's
  507.         ** specification of the reduction which is just about
  508.         ** to be done.
  509.         */
  510.         if ( zzdebug )
  511.             printf( "Reduce by (%d) \"%s\"\n",
  512.                 zz_n, zzreds[ zz_n ] );
  513. #endif
  514.         zztmp = zz_n;            /* value to switch over */
  515.         zzpvt = zz_pv;            /* $vars top of value stack */
  516.         /*
  517.         ** Look in goto table for next state
  518.         ** Sorry about using zz_state here as temporary
  519.         ** register variable, but why not, if it works...
  520.         ** If zzr2[ zz_n ] doesn't have the low order bit
  521.         ** set, then there is no action to be done for
  522.         ** this reduction.  So, no saving & unsaving of
  523.         ** registers done.  The only difference between the
  524.         ** code just after the if and the body of the if is
  525.         ** the goto zz_stack in the body.  This way the test
  526.         ** can be made before the choice of what to do is needed.
  527.         */
  528.         {
  529.             /* length of production doubled with extra bit */
  530.             register int zz_len = zzr2[ zz_n ];
  531.  
  532.             if ( !( zz_len & 01 ) )
  533.             {
  534.                 zz_len >>= 1;
  535.                 zzval = ( zz_pv -= zz_len )[1];    /* $$ = $1 */
  536.                 zz_state = zzpgo[ zz_n = zzr1[ zz_n ] ] +
  537.                     *( zz_ps -= zz_len ) + 1;
  538.                 if ( zz_state >= YYLAST ||
  539.                     zzchk[ zz_state =
  540.                     zzact[ zz_state ] ] != -zz_n )
  541.                 {
  542.                     zz_state = zzact[ zzpgo[ zz_n ] ];
  543.                 }
  544.                 goto zz_stack;
  545.             }
  546.             zz_len >>= 1;
  547.             zzval = ( zz_pv -= zz_len )[1];    /* $$ = $1 */
  548.             zz_state = zzpgo[ zz_n = zzr1[ zz_n ] ] +
  549.                 *( zz_ps -= zz_len ) + 1;
  550.             if ( zz_state >= YYLAST ||
  551.                 zzchk[ zz_state = zzact[ zz_state ] ] != -zz_n )
  552.             {
  553.                 zz_state = zzact[ zzpgo[ zz_n ] ];
  554.             }
  555.         }
  556.                     /* save until reenter driver code */
  557.         zzstate = zz_state;
  558.         zzps = zz_ps;
  559.         zzpv = zz_pv;
  560.     }
  561.     /*
  562.     ** code supplied by user is placed in this switch
  563.     */
  564.     switch( zztmp )
  565.     {
  566.         
  567. case 1:
  568. # line 62 "posixtm.y"
  569. {
  570.              if (zzpvt[-5] >= 1 && zzpvt[-5] <= 12)
  571.            t.tm_mon = zzpvt[-5] - 1;
  572.          else {
  573.            YYABORT;
  574.          }
  575.          if (zzpvt[-4] >= 1 && zzpvt[-4] <= 31)
  576.            t.tm_mday = zzpvt[-4];
  577.          else {
  578.            YYABORT;
  579.          }
  580.          if (zzpvt[-3] >= 0 && zzpvt[-3] <= 23)
  581.            t.tm_hour = zzpvt[-3];
  582.          else {
  583.            YYABORT;
  584.          }
  585.          if (zzpvt[-2] >= 0 && zzpvt[-2] <= 59)
  586.            t.tm_min = zzpvt[-2];
  587.          else {
  588.            YYABORT;
  589.          }
  590.            } break;
  591. case 2:
  592. # line 85 "posixtm.y"
  593. {
  594.                    t.tm_year = zzpvt[-0];
  595.            /* Deduce the century based on the year.
  596.               See POSIX.2 section 4.63.3.  */
  597.            if (zzpvt[-0] <= 68)
  598.              t.tm_year += 100;
  599.          } break;
  600. case 3:
  601. # line 92 "posixtm.y"
  602. {
  603.                             t.tm_year = zzpvt[-1] * 100 + zzpvt[-0];
  604.                 if (t.tm_year < 1900) {
  605.                   YYABORT;
  606.                 } else
  607.                   t.tm_year -= 1900;
  608.               } break;
  609. case 4:
  610. # line 99 "posixtm.y"
  611. {
  612.                     time_t now;
  613.             struct tm *tmp;
  614.  
  615.                     /* Use current year.  */
  616.                     time (&now);
  617.             tmp = localtime (&now);
  618.             t.tm_year = tmp->tm_year;
  619.           } break;
  620. case 5:
  621. # line 110 "posixtm.y"
  622. {
  623.                         t.tm_sec = 0;
  624.               } break;
  625. case 6:
  626. # line 113 "posixtm.y"
  627. {
  628.                       if (zzpvt[-0] >= 0 && zzpvt[-0] <= 61)
  629.                 t.tm_sec = zzpvt[-0];
  630.               else {
  631.                 YYABORT;
  632.               }
  633.             } break;
  634. case 7:
  635. # line 122 "posixtm.y"
  636. {
  637.                           zzval = zzpvt[-1] * 10 + zzpvt[-0];
  638.             } break;
  639.     }
  640.     goto zzstack;        /* reset registers in driver code */
  641. }
  642.